/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle.paint.boggle;

import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.paint.iDrawable;

public class BoggleSelectedLettersBitmapDrawable extends iDrawable {
  private final Word myWord;
  private int myColor;
  private Paint myPaint = new Paint();
  
  private Bitmap myBitMap = null;
  
  private int myBitMapX;
  private int myBitMapY;
  private int myBitMapWidth;
  private int myBitMapHeight;
  
  public BoggleSelectedLettersBitmapDrawable( Word aWord, int aColor) {
    myWord = aWord;
    myColor = aColor;
    init();
  }
  
  private void init(){
    myPaint.setColor( myColor );
    myPaint.setStrokeWidth( 18 );
    myPaint.setStyle(Style.FILL);
    myPaint.setAntiAlias(true);
    myPaint.setMaskFilter( new BlurMaskFilter( 5, BlurMaskFilter.Blur.NORMAL) );
    myPaint.setAlpha(60);
  }
  
  private void detectSizes(WordBattleView aView){
    float theTopLeftX = Float.MAX_VALUE;
    float theBottomRightX = Float.MIN_VALUE;
    float theTopLeftY = Float.MAX_VALUE;
    float theBottomRightY = Float.MIN_VALUE;

    for(Letter theLetter : myWord.getLetters()){

      float theLetterTopLeftX = WordBattleView.BORDER_SIZE + theLetter.getColumn() * aView.getWidthPerColumn();
      float theLetterBottomRightX = WordBattleView.BORDER_SIZE + (theLetter.getColumn()  + 1 )* aView.getWidthPerColumn();
      float theLetterTopLeftY = WordBattleView.BORDER_SIZE + theLetter.getRow() * aView.getHeightPerRow();
      float theLetterBottomRightY = WordBattleView.BORDER_SIZE + (theLetter.getRow() + 1) * aView.getHeightPerRow();

      if(theLetterTopLeftX < theTopLeftX) theTopLeftX = theLetterTopLeftX;
      if(theLetterTopLeftY < theTopLeftY) theTopLeftY = theLetterTopLeftY;

      if(theLetterBottomRightX > theBottomRightX) theBottomRightX = theLetterBottomRightX;
      if(theLetterBottomRightY > theBottomRightY) theBottomRightY = theLetterBottomRightY;
    }

    myBitMapX = (int)(theTopLeftX - aView.getWidthPerColumn());
    myBitMapY = (int)(theTopLeftY - aView.getHeightPerRow());
    myBitMapWidth = (int)(theBottomRightX - theTopLeftX + 2 * aView.getWidthPerColumn());
    myBitMapHeight = (int)(theBottomRightY - theTopLeftY + 2 * aView.getHeightPerRow());
  }
  
  private void createBitmap(WordBattleView aView){
    myBitMap = Bitmap.createBitmap( (int)aView.getWidthPerColumn(), (int)aView.getHeightPerRow(), Bitmap.Config.ALPHA_8 );
    Canvas theCanvas = new Canvas(myBitMap);
    
//    myPicture = new Picture();
//    Canvas theCanvas = myPicture.beginRecording((int)myWordBattleView.getWidthPerColumn(), (int)myWordBattleView.getHeightPerRow());
    
    Letter thePreviousLetter = null;
    float thePreviousCenterX = -1;
    float thePreviousCenterY = -1;
    for(Letter theLetter : myWord.getLetters()){
      
      float theCenterX = (float)(WordBattleView.BORDER_SIZE + (theLetter.getColumn() + 0.5) * aView.getWidthPerColumn());
      float theCenterY = (float)(WordBattleView.BORDER_SIZE + (theLetter.getRow() + 0.5) * aView.getHeightPerRow());
      
      if(thePreviousLetter != null){
        //draw a rectangle from the center of the previsou letter to the center of this letter
        theCanvas.drawLine(thePreviousCenterX, thePreviousCenterY, theCenterX, theCenterY, myPaint);
      }
      
      thePreviousLetter = theLetter;
      thePreviousCenterX = theCenterX;
      thePreviousCenterY = theCenterY;
    }
    
  }
  
  @Override
  public void paint( Canvas aCanvas, View aView ) {
    if(myBitMap == null) {
      detectSizes( (WordBattleView)aView );
      createBitmap((WordBattleView)aView);
    }
    aCanvas.drawBitmap(myBitMap, myBitMapX, myBitMapY, myPaint);
  }
}
